#include <iostream>
#include<vector>
#include<queue>
using namespace std;

int dx[] = { -1,1,0,0 };
int dy[] = { 0,0,-1,1 };
int n, m;
int x1, y1, x2, y2;

int bfs(int i, int j, vector<vector<char>>& vv, vector<vector<int>>& visit)
{
    if (vv[x2][y2] == '*') return -1;
    //cout<<i<<" "<<j<<endl;
    queue<pair<int, int>> q;
    q.push({ i,j });
    while (q.size())
    {
        auto [a, b] = q.front();
        q.pop();
        for (int k = 0;k < 4;k++)
        {
            int x = a + dx[k], y = b + dy[k];
            if (x >= 1 && x <= n && y >= 0 && y <= m && vv[x][y] == '.' && visit[x][y] == -1)
            {
                visit[x][y] = visit[a][b] + 1;
                if (x == x2 && y == y2)
                {
                    return visit[x2][y2];
                }
                q.push({ x,y });
            }
        }
    }
    return -1;




}

int main() {
    cin >> n >> m;
    vector<vector<char>> vv(n + 1, vector<char>(m + 1));
    cin >> x1 >> y1 >> x2 >> y2;
    for (int i = 1;i <= n;i++)
    {
        for (int j = 1;j <= m;j++)
        {
            cin >> vv[i][j];
        }
    }
    vector<vector<int>> visit(n + 1, vector<int>(m + 1, -1));
    visit[x1][y1] = 0;
    cout << bfs(x1, y1, vv, visit);

    return 0;



}